package com.fyzk.activity.trend;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.fyzk.activity.app.AppToast;
import com.tyjc.lottery.R;

import java.util.List;

/**
 * Created by fanxs11 on 2017/7/20.
 * 快乐扑克走势图
 */

public class KLPKTrendView extends TrendBaseView {

    private int chooseId = R.id.trend_choose_rb1;
    private int payType = 0;

    public KLPKTrendView(Context context) {
        super(context);
    }

    public KLPKTrendView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public KLPKTrendView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initData() {
        rowVisibleCount = 13;
        topPaddingCount = 1;
        leftPpaddingCount = 2;
        scale = 1.0f;
        rowCount = 13;
        super.initData();
    }

    @Override
    boolean isShowBottom(List<Drawinfor> infors) {
        return false;
    }

    @Override
    protected void onDrawTop(Canvas canvas, Rect rect) {
        paintBg.setColor(topBg);
        super.onDrawTop(canvas, rect);
        Rect topRect = new Rect(rect.left,rect.top, rect.left, rect.top);

        paintLine.setColor(lineColor);
        switch (chooseId) {
            case R.id.trend_choose_rb1:     // 第一个选项卡——开奖

                topRect.set(topRect.right, topRect.top, topRect.right+unitLength*3,topRect.top+unitLength);
                drawText(canvas, paintTop, "开奖号", topRect);
                paintLine.setColor(lineColor);
                canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);

                for (int i = 0; i < rowCount; i++) {
                    topRect.set(topRect.right, topRect.top, topRect.right+unitLength,topRect.top+unitLength);
                    String num = ""+(i+1);
                    num = getKLPKNum(num, false);
                    drawText(canvas, paintTop, ""+num, topRect);
                    if (i < rowCount-1) {
                        canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                    }
                }
                break;
            case R.id.trend_choose_rb2:     // 第二个选项卡——基本走势
                switch (payType) {
                    case 0:     // 0-5:任选n
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 11:    // 11-15:任选n胆拖
                    case 12:
                    case 13:
                    case 14:
                    case 15:
                        String[] titles = {"和值","跨度","大小比","奇偶比","质合比"};
                        for (int i = 0; i < titles.length; i++) {
                            topRect.set(topRect.right, topRect.top, (int) (topRect.right+unitLength*2.2),topRect.top+unitLength);
                            String num = ""+titles[i];
                            drawText(canvas, paintTop, ""+num, topRect);
                            if (i < titles.length-1) {
                                canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                            }
                        }
                        break;
                    case 6:     // 猜对子
                    case 7:     // 猜豹子
                    case 8:     // 猜顺子
                        topRect.set(topRect.right, topRect.top, topRect.right+unitLength*2,topRect.top+unitLength);
                        drawText(canvas, paintTop, "开奖号", topRect);
                        canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                        String[] titles2 = {"对子","豹子","同花","顺子","同花顺","散牌"};
                        for (int i = 0; i < titles2.length; i++) {
                            topRect.set(topRect.right, topRect.top, (int) (topRect.right+unitLength*1.5),topRect.top+unitLength);
                            String num = ""+titles2[i];
                            drawText(canvas, paintTop, ""+num, topRect);
                            if (i < titles2.length-1) {
                                canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                            }
                        }
                        break;
                    case 9:     // 猜同花
                    case 10:    // 猜同花顺
                        topRect.set(topRect.right, topRect.top, topRect.right+unitLength*3,topRect.top+unitLength);
                        drawText(canvas, paintTop, "开奖号", topRect);
                        canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                        String[] titles3 = {"同花","黑桃","红桃","梅花","方块"};
                        for (int i = 0; i < titles3.length; i++) {
                            topRect.set(topRect.right, topRect.top, (int) (topRect.right+unitLength*1.6),topRect.top+unitLength);
                            String num = ""+titles3[i];
                            drawText(canvas, paintTop, ""+num, topRect);
                            if (i < titles3.length-1) {
                                canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                            }
                        }
                        break;
                    default:
                        break;
                }
                break;
            case R.id.trend_choose_rb3:     // 第三个选项卡——和值走势（或 形态走势  或 号码分布）
                topRect.set(topRect.right, topRect.top, (int) (topRect.right+unitLength*2.5),topRect.top+unitLength);
                drawText(canvas, paintTop, "开奖号", topRect);
                paintLine.setColor(lineColor);
                canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);

                topRect.set(topRect.right, topRect.top, (int) (topRect.right+unitLength*1.5),topRect.top+unitLength);
                drawText(canvas, paintTop, "对子", topRect);
                paintLine.setColor(lineColor);
                canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);

                for (int i = 0; i < rowCount; i++) {
                    topRect.set(topRect.right, topRect.top, topRect.right+unitLength,topRect.top+unitLength);
                    String num = ""+(i+1);
                    num = getKLPKNum(num, true);
                    drawText(canvas, paintTop, ""+num, topRect);
                    if (i < rowCount-1) {
                        canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                    }
                }
                break;
            default:
                break;
        }

        paintLine.setColor(textDefault);
        canvas.drawLine(rect.left, rect.bottom-1, rect.right, rect.bottom-1, paintLine);
    }

    @Override
    protected void onDrawCenter(Canvas canvas, int left, int top) {
        switch (chooseId) {
            case R.id.trend_choose_rb1:
                drawCenter1(canvas, left, top);
                break;
            case R.id.trend_choose_rb2:
                drawCenter2(canvas, left, top);
                break;
            case R.id.trend_choose_rb3:
                drawCenter3(canvas, left, top);
                break;
            default:
                super.onDrawCenter(canvas,left,top);
                break;
        }
    }

    private void drawCenterBg(Canvas canvas, int left, int top, int i) {
        Rect bgRect = new Rect(left, top+i*unitLength, drawWidth, top+(i+1)*unitLength);
        if (i % 2 == 0) {
            paintBg.setColor(bgBai);
        } else {
            paintBg.setColor(bgHui);
        }

        canvas.drawRect(bgRect, paintBg);
        paintLine.setColor(textDefault);
        canvas.drawLine(bgRect.left, bgRect.bottom, bgRect.right, bgRect.bottom, paintLine);
    }

    // 第一个选项卡——号码分布
    private void drawCenter1(Canvas canvas, int left, int top) {
        Rect topRect = new Rect(left,top,left,top);
        for (int i = 0; i < columnCount; i++) {
            topRect.set(left,topRect.bottom,left,topRect.bottom+unitLength);
            drawCenterBg(canvas, left, top, i);

            topRect.set(topRect.right, topRect.top, topRect.right+unitLength*3,topRect.top+unitLength);
            drawText(canvas, paintTop, "2 3 4", topRect);
            paintLine.setColor(lineColor);
            canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);

            for (int j = 0; j < rowCount; j++) {
                topRect.set(topRect.right, topRect.top, topRect.right+unitLength,topRect.top+unitLength);
                String num = ""+(j+1);
                drawText(canvas, paintTop, ""+num, topRect);
                if (j < rowCount-1) {
                    paintLine.setColor(lineColor_L);
                    canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                }
            }
        }
    }

    // 第二个选项卡——形态统计、形态走势
    private void drawCenter2(Canvas canvas, int left, int top) {
        Rect topRect = new Rect(left,top,left,top);
        for (int i = 0; i < columnCount; i++) {
            drawCenterBg(canvas, left, top, i);

            topRect.set(left,topRect.bottom,left,topRect.bottom+unitLength);
            paintLine.setColor(lineColor);

            switch (payType) {
                case 0:     // 0-5:任选n
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 11:    // 11-15:任选n胆拖
                case 12:
                case 13:
                case 14:
                case 15:
                    String[] titles = {"9","4","2:3","1:2","4:5"};
                    for (int j = 0; j < titles.length; j++) {
                        topRect.set(topRect.right, topRect.top, (int) (topRect.right+unitLength*2.2),topRect.top+unitLength);
                        String num = ""+titles[j];
                        drawText(canvas, paintTop, ""+num, topRect);
                        if (j < titles.length-1) {
                            canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                        }
                    }
                    break;
                case 6:     // 猜对子
                case 7:     // 猜豹子
                case 8:     // 猜顺子
                    topRect.set(topRect.right, topRect.top, topRect.right+unitLength*2,topRect.top+unitLength);
                    drawText(canvas, paintTop, "2 3 4", topRect);
                    canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                    String[] titles2 = {"对子","豹子","同花","顺子","同花顺","散牌"};
                    for (int j = 0; j < titles2.length; j++) {
                        topRect.set(topRect.right, topRect.top, (int) (topRect.right+unitLength*1.5),topRect.top+unitLength);
                        String num = ""+titles2[j];
                        drawText(canvas, paintTop, ""+num, topRect);
                        if (j < titles2.length-1) {
                            paintLine.setColor(lineColor_L);
                            canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                        }
                    }
                    break;
                case 9:     // 猜同花
                case 10:    // 猜同花顺
                    topRect.set(topRect.right, topRect.top, topRect.right+unitLength*3,topRect.top+unitLength);
                    drawText(canvas, paintTop, "2 3 4", topRect);
                    canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                    String[] titles3 = {"同花","黑桃","红桃","梅花","方块"};
                    for (int j = 0; j < titles3.length; j++) {
                        topRect.set(topRect.right, topRect.top, (int) (topRect.right+unitLength*1.6),topRect.top+unitLength);
                        String num = ""+titles3[j];
                        drawText(canvas, paintTop, ""+num, topRect);
                        if (j < titles3.length-1) {
                            paintLine.setColor(lineColor_L);
                            canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }

    // 第三个选项卡—— 对子走势（玩法：对子 独有）
    private void drawCenter3(Canvas canvas, int left, int top) {
        Rect topRect = new Rect(left,top,left,top);
        for (int i = 0; i < columnCount; i++) {
            drawCenterBg(canvas, left, top, i);

            topRect.set(left,topRect.bottom,left,topRect.bottom+unitLength);
            paintLine.setColor(lineColor);

            topRect.set(topRect.right, topRect.top, (int) (topRect.right+unitLength*2.5),topRect.top+unitLength);
            drawText(canvas, paintTop, "2 3 4", topRect);
            canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);

            topRect.set(topRect.right, topRect.top, (int) (topRect.right+unitLength*1.5),topRect.top+unitLength);
            drawText(canvas, paintTop, "对子", topRect);
            canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);

            for (int j = 0; j < rowCount; j++) {
                topRect.set(topRect.right, topRect.top, topRect.right+unitLength,topRect.top+unitLength);
                String num = ""+(j+1);
                drawText(canvas, paintTop, ""+num, topRect);
                if (j < rowCount-1) {
                    paintLine.setColor(lineColor_L);
                    canvas.drawLine(topRect.right-1, topRect.top, topRect.right-1, topRect.bottom, paintLine);
                }
            }
        }
    }

    /**
     * 获取快乐扑克 号码 A 2 3 ... J Q K
     * @param num
     * @param isDuiZi   true：返回对子 AA 11 22 ... JJ QQ KK
     * @return
     */
    private String getKLPKNum(String num, boolean isDuiZi) {
        String klpk_num = num;
        if (num.equals("1")) {
            klpk_num = "A";
        } else if (num.equals("11")) {
            klpk_num = "J";
        } else if (num.equals("12")) {
            klpk_num = "Q";
        } else if (num.equals("13")) {
            klpk_num = "K";
        }

        if (isDuiZi) {
            klpk_num = klpk_num+klpk_num;
        }
        return klpk_num;
    }

    @Override
    public void onChooseClick(int id) {
        chooseId = id;
        setRowCount(16);
        switch (id) {
            case R.id.trend_choose_rb1:
                break;
            case R.id.trend_choose_rb2:
                setRowCount(11);
                setColumnCount(50, false);
                break;
            case R.id.trend_choose_rb3:
                setRowCount(17);
                setColumnCount(50, false);
                break;
            default:
                break;
        }
        setScrollX(0);
        setScrollY(drawHeight - getHeight());
        recording();
        invalidate();
    }

    @Override
    public void onChangePayType(int id) {
        AppToast.getToast().show("玩法："+id);
        payType = id;
    }
}
